// Copyright (c) 2007 Robin Boerdijk. All rights reserved.
//
// This software is licensed under the terms of the Microsoft Reciprocal License as described 
// in the file LICENSE.txt, which you should have received as part of this distribution. The 
// license terms are also available at http://www.opensource.org/licenses/ms-rl.html.

#include "demo/Calculator.h"

#include "wtk/Boxes.h"

namespace demo
{
	namespace
	{
		const wchar_t *Plus = L"\x002B";
		const wchar_t *Minus = L"\x2212";
		const wchar_t *Multiplication = L"\x00D7";
		const wchar_t *Division = L"\x00F7";
		const wchar_t *SquareRoot = L"\x221A";
		const wchar_t *Equals = L"\x003D";
		const wchar_t *PlusMinus = L"\x002B/\x2212";
		const wchar_t *DecimalPoint = L"\x002E";
		const wchar_t *Percent = L"\x0025";
		const wchar_t *Inverse = L"\x00B9/x";
	}

	Calculator::Calculator(wtk::Window *parentWindow)
		: wtk::DialogWindow(parentWindow, L"Calculator")
		, m_display(this, 12)
	{
		static const wchar_t *ButtonStrings[] = {
			L"7", L"8",	L"9", Division, SquareRoot, 
			L"4", L"5", L"6", Multiplication, Percent,
			L"1", L"2", L"3", Minus, Inverse,
			L"0", PlusMinus, DecimalPoint, Plus, Equals
		};

		for (int i = 0; i < 20; i++) {
			m_buttons[i].reset(new wtk::PushButton(this, ButtonStrings[i]));
		}
	}

	wtk::Box *Calculator::CreateClientBox()
	{
		wtk::VerticalBox *clientBox = new wtk::VerticalBox(2);
		clientBox->SetMargin(2, 2);

		wtk::ControlBox *displayBox = clientBox->AddControlBox(m_display, 1, 0);
		displayBox->SetMargin(2, 2);

		wtk::GridBox *buttonGrid = clientBox->AddGridBox(5, 4, wtk::GridBox::EqualizeColumnWidths | wtk::GridBox::EqualizeRowHeights);

		for (int i = 0; i < 20; i++) {
			wtk::ControlBox *buttonBox = buttonGrid->AddControlBox(*m_buttons[i], 1, 1);
			buttonBox->SetMargin(2, 1);
		}

		return clientBox;
	}
}
